package com.the_ring.chapter15.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;

import androidx.annotation.Nullable;

import java.util.List;

public class ViewSlider extends View {

    private Context context;

    private float lastX = 0;           // 上次按下点的横坐标
    private int pos = 0;                // 当前书页的序号
    private PageView preView, currentView, nextView;    // 上一个视图、当前视图、下一个视图
    private int showPage;               // 显示页面类型
    private int direction;              // 滑动方向
    private Scroller scroller;          // 一个滑动器对象
    private boolean isScrolling = false;    // 是否正在滚动

    private final int SHOW_PRE = 0;
    private final int SHOW_NEXT = 1;

    private final int DIRECTION_LEFT = 0;
    private final int DIRECTION_RIGHT = 1;

    private int width; // 布局宽度

    private List<String> pathList;


    public ViewSlider(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        // 创建一个基于线性插值器的滚动对象
        scroller = new Scroller(context, new LinearInterpolator());
        width = getMeasuredWidth();
    }

    public void setPathList(List<String> pathList) {
        this.pathList = pathList;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!scroller.isFinished() && isScrolling) {
            // 正在滚动则忽略触摸事件
            return super.onTouchEvent(event);
        }
        int distanceX = (int) (event.getRawX() - lastX);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getRawX();
                break;
            case MotionEvent.ACTION_MOVE:
                if (distanceX > 0) {
                    // 右滑，拉出上一页
                    if (pos != 0) {
                        showPage = SHOW_PRE;
                        preView.setUp(true);
                        preView.setMargin(-width + distanceX);
                        currentView.setUp(false);
                    }
                } else {
                    // 左滑。下一页
                    if (pos < pathList.size() -  1) {
                        showPage = SHOW_NEXT;
                        currentView.setMargin(distanceX);
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                if ((pos == 0 && distanceX > 0) ||
                        (pos == pathList.size() - 1 && distanceX < 0)) {
                    // 第一页不准往前翻，最后一页不准往后翻
                    break;
                }
                isScrolling = true;
                if (showPage == SHOW_PRE) {
                    // 在拉出上一页
                    direction = Math.abs(distanceX) < width / 2 ? DIRECTION_RIGHT : DIRECTION_RIGHT;
                    int distance = direction == DIRECTION_LEFT ? -distanceX : width - distanceX;
                    scroller.startScroll(-width + distanceX, 0, distance, 0, 400);
                } else if (showPage == SHOW_NEXT) {
                    direction = Math.abs(distanceX) > width / 2 ? DIRECTION_LEFT : DIRECTION_RIGHT;
                    int distance = direction == DIRECTION_RIGHT ? -distanceX : -(width + distanceX);
                    scroller.startScroll(distanceX, 0, distance, 0, 400);
                }
                break;
        }
        return true;
    }

    // 滑动过程中触发
    @Override
    public void computeScroll() {
        if (scroller.computeScrollOffset()) {
            PageView view = showPage == SHOW_PRE ? preView : currentView;
            view.setMargin(scroller.getCurrX());
            if (scroller.getFinalX() == scroller.getCurrX()) {
                onScrollEnd(direction);
                isScrolling = false;
            }
        }
    }

    private void onScrollEnd(int direction) {

    }
}
